package com.yyn.Learning.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yyn.Learning.entity.EduSubject;
import com.yyn.Learning.entity.Subject.OneLevel;
import com.yyn.Learning.entity.Subject.TwoLevel;
import com.yyn.Learning.entity.excel.SubjectData;
import com.yyn.Learning.listener.SubjectExcelListener;
import com.yyn.Learning.mapper.EduSubjectMapper;
import com.yyn.Learning.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yyn.R;
import io.swagger.annotations.ApiOperation;
import jdk.jfr.internal.Utils;
import org.apache.ibatis.annotations.One;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.security.auth.Subject;
import java.io.InputStream;
import java.sql.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author yyn
 * @since 2022-11-16
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //    添加课程分类
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
        try {
//            获取到文件输入流
            InputStream inputStream = file.getInputStream();
//            进行文件读取(文件,excel实体类,监听类)
            EasyExcel.read(inputStream, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    @ApiOperation("课程管理列表")
    public List getAllSubjectList() {
//        查询一级所有分类
        LambdaQueryWrapper<EduSubject> oneWrapper = new LambdaQueryWrapper<EduSubject>();
        oneWrapper.eq(EduSubject::getParentId, "0");
        List<EduSubject> oneAllLevelList = baseMapper.selectList(oneWrapper);
//        查询二级所有分类
        LambdaQueryWrapper<EduSubject> twoWrapper = new LambdaQueryWrapper<>();
        twoWrapper.ne(EduSubject::getParentId, "0");
        List<EduSubject> twoAllLevelList = baseMapper.selectList(twoWrapper);
//        创建一个集合用于接收最终封装好的数据
        List<OneLevel> finalSubject = new ArrayList<OneLevel>();
        //        封装一级分类
//        方法一  遍历
//        for(int i=0;i<oneAllLevelList.size();i++){
////            获取到
//            EduSubject eduSubject = oneAllLevelList.get(i);
////            设置一级分类的值  里面包含有二级类
//            OneLevel oneLevel = new OneLevel();
////            把循环出来的值存到一级分类中
//            oneLevel.setId(oneLevel.getId());
//            finalSubject.add(oneLevel);
//
//        }
//        stream流实现
        oneAllLevelList = oneAllLevelList.stream().map(item -> {
            OneLevel oneLevel = new OneLevel();
//            oneLevel.setId(item.getId());
//           免除了 get set方法  直接把该信息里信息 赋值给oneLevel对应的信息
            BeanUtils.copyProperties(item, oneLevel);
            //                    用与级联选择器
            oneLevel.setValue(oneLevel.getId());
            oneLevel.setLabel(oneLevel.getTitle());
            finalSubject.add(oneLevel);

//            封装二级分类
            ArrayList<TwoLevel> twoFinalLevels = new ArrayList<TwoLevel>();
            for (int i = 0; i < twoAllLevelList.size(); i++) {
                EduSubject eduSubject = new EduSubject();
                eduSubject= twoAllLevelList.get(i);
                if (eduSubject.getParentId().equals(oneLevel.getId())) {
                    TwoLevel twoLevel = new TwoLevel();
                    BeanUtils.copyProperties(eduSubject, twoLevel);
//                    用与级联选择器
                    twoLevel.setValue(twoLevel.getId());
                    twoLevel.setLabel(twoLevel.getTitle());
                    twoFinalLevels.add(twoLevel);
                }
                oneLevel.setChildren(twoFinalLevels);
            }

            return item;
        }).collect(Collectors.toList());
        return finalSubject;
    }
@Autowired
EduSubjectMapper eduSubjectMapper;
    @Override
    public String getSubjectById(String id) {
        String subjectById = eduSubjectMapper.getSubjectById(id);
        return subjectById;
    }
}
